﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using ResxCrunch.Business.Managers;
using ResxCrunch.Properties;

namespace ResxCrunch
{
    /// <summary>
    /// Activate license form class
    /// </summary>
    public partial class FrmActivateLicense : Form
    {
        #region Constants
        /// <summary>
        /// Logger declaration
        /// </summary>
        private static readonly ILog log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        #endregion Constants

        /// <summary>
        /// Initializes a new instance of the <see cref="FrmActivateLicense"/> class.
        /// </summary>
        public FrmActivateLicense()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the btnActivate control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnActivate_Click(object sender, EventArgs e)
        {
            bool isSerialValid = SerialMngr.Instance.VerifySerial(txtSerial.Text);

            if (isSerialValid)
            {
                //if (SerialMngr.Instance.GenerateLicenseFile(txtSerial.Text, Settings.Default.DataFilePath))
                //{
                //    MessageBox.Show("ResxCrunch was successfully registered! Please hold on to the serial for future use.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                //}
                //else
                //{
                //    MessageBox.Show("Registration was valid but registration file could not be edited. Please verify if the application's install folder is writeable and try again.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                //}

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("The serial is invalid. Please verify if you typed it correctly and try again. If you keep having issues, please contact us.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void FrmActivateLicense_Load(object sender, EventArgs e)
        {
            //txtSerial.Text = SerialMngr.Instance.GenerateSerial("contact@resxcrunch.com", 0, 5);
        }

        private void tsbLoadKetFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "Key files (*.key)|*.key|All files (*.*)|*.*";
            ofd.FilterIndex = 0;
            ofd.RestoreDirectory = true;

            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string file = ofd.FileName;

                try
                {
                    using (StreamReader sr = new StreamReader(file))
                    {
                        string serial = sr.ReadToEnd();

                        if (!string.IsNullOrEmpty(serial))
                        {
                            txtSerial.Text = serial;
                        }
                        else
                        {
                            MessageBox.Show(this, "No key could be loaded from the file.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                }
                catch (Exception ex)
                {
                    log.Error(string.Format("Could not load keyfile from '{0}'", file), ex);
                    MessageBox.Show(this, "Key could not be loaded, please verify if the file is not corrupt and you have enough priviledges to access it.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    return;
                }
            }
        }

        private void tsbVerify_Click(object sender, EventArgs e)
        {
            bool isSerialValid = SerialMngr.Instance.VerifySerial(txtSerial.Text);

            if (isSerialValid)
            {
                MessageBox.Show("ResxCrunch was successfully registered! Please hold on to the serial for future use.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("The serial is invalid. Please verify if you typed it correctly and try again. If you keep having issues, please contact us.", Settings.Default.AppName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
